package com.cqut.forum.dao.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ArticleGroupDao;
import com.cqut.forum.entity.ArticleGroup;

/**
 * 图片分组dao实现类
 * @author yangxun
 */
@Component
public class ArticleGroupDaoImp extends BaseDaoImp implements ArticleGroupDao {

	@SuppressWarnings("unchecked")
	@Override
	public ArticleGroup getByName(String agName) {
		String hql = "select ag from ArticleGroup ag where ag.articleGroupName = '"+agName+"'";
		List<ArticleGroup> result = super.getHibernateTemplate().find(hql);
		return result.size()>0?result.get(0):null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ArticleGroup> getGroups(int userId,int index,int size) {
		String hql = "select ag from ArticleGroup ag where ag.ownerId = ? and ag.articleGroupState=true";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		Map<String,Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<ArticleGroup> result = (List<ArticleGroup>) super.excuteHql(hql, parameters, pages);
		
		return result;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> loadGroupsByModuleName(String moduleName) {
		String sql = "select ag.article_group_id,ag.article_group_name,ag.article_password," +
				"ag.article_number,ag.is_public,ag.article_group_state from article_group ag,module m " +
				"where ag.module_id=m.module_id and m.module_name=? and ag.article_group_state=true ";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(moduleName);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		
		return result;
	}
}
